package com.zhdl.network.servertcp;

import com.project.network.conf.LinkServerSocketBuild;
import com.project.network.entity.PortSocket;
import com.project.network.server.SerialSocketServer;
import com.zhdl.network.demotcp.dto.MemoBusPacket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static com.project.common.constant.Dic.HAND_MODBUS_RTU_ANALYSIS;
import static com.project.network.server.SerialSocketServer.tioServerConfig;

/**
 * @Author: zhangyy
 * @Email: zhang10092009@hotmail.com
 * @Date: 2023/8/3 17:42:25
 * @Version: v1.0
 * @Modified:
 * @Description:
 */
@Slf4j
@Component
public class HandServerSocketBuild extends LinkServerSocketBuild {

    @Override
    protected SerialSocketServer buildHandServer(PortSocket properties) throws IOException {
        return buildServer(properties);
    }

    private SerialSocketServer buildServer(PortSocket portSocket) throws IOException {
        String protocols = portSocket.getProtocols();
        String serverIp = portSocket.getServerIp();
        String serverPort = portSocket.getServerPort();
        ModBusTcpServerHandler handler = new ModBusTcpServerHandler(serverIp, serverPort, protocols, message);
        ServerTcpListener listener = new ServerTcpListener();
        MemoBusPacket packet = new MemoBusPacket();
        SerialSocketServer socket = new SerialSocketServer(handler, listener, packet, serverIp, serverPort);
        socket.openServerSocket(HAND_MODBUS_RTU_ANALYSIS, serverIp, Integer.parseInt(serverPort),(1000*60), handler, listener);
//        tioServerConfig.setHeartbeatTimeout(1000*60);
        return socket;
    }
}
